package 栈和队列;

import java.util.Stack;

public class IsValid {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<s.length();i++){
            System.out.println("i="+i+":"+stack.peek());
            char c = s.charAt(i);
            if(c=='(' || c=='{' || c=='[' || stack.isEmpty()){
                stack.push(c);
            }else if(c==')' || c=='}' || c==']'){
                char peek = stack.peek();
                System.out.println("peek="+peek+",c="+c);
                if((peek=='('&&c==')') || (peek=='{'&&c=='}') || (peek=='['&&c==']')){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        System.out.println(stack.isEmpty());
        if(!stack.isEmpty())return false;
        return true;
    }
}
